﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

using System.Web.Security;

namespace loveMotorcade.web.Classes
{
    public static class staticClassExt
    {
        /// <summary>
        /// 进行MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MD5(this string str)
        {
            if (str == null) { str = ""; }
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str.Trim(), "MD5");
        }
        public static string ToTryString(this object obj)
        {
            return obj == null ? null : obj.ToString();
        }

        public static int? ToInt(this object obj)
        {
            if (obj == null) return null;

            try
            {
                return Convert.ToInt32(obj);
            }
            catch
            {
                return null;
            }
        }

        public static DateTime? ToDateTime(this object obj)
        {
            try
            {
                return Convert.ToDateTime(obj);
            }
            catch
            {
                return null;
            }
        }
        public static string[] tryToArray(this string str,string splitStr)
        {
            if (!String.IsNullOrWhiteSpace(str))
            {
                return str.Split(new string[]{splitStr},StringSplitOptions.RemoveEmptyEntries);
            }
            return new string[]{};

        }
        public static string[] tryToArray(this IEnumerable<string> ls)
        {
            if (ls != null)
            {
                return ls.ToArray<string>();
            }
            return new string[] { };

        }
        public static string tryJoin(this IEnumerable<string> ls, string splitStr)
        {
            if (ls != null)
            {
                return String.Join(splitStr, ls);
            }
            return "";

        }
        public static int? tryToInt(this string str)
        {
            try
            {
                return Convert.ToInt32(str);
            }
            catch
            {
                return null;
            }
        }
    }
}